<?php

namespace app\index\controller\wanlshop;

use app\common\controller\Wanlshop;

/**
 * 主页
 * @internal
 */
class Console extends Wanlshop
{
    protected $noNeedLogin = '';
    protected $noNeedRight = '*';
    
    public function _initialize()
    {
        parent::_initialize();
    }
    
    public function index()
    {
		$shop_id = $this->shop->id;
		$seventtime = \fast\Date::unixtime('day', -6);
        $userType =  $this->request->param('user_type',0);
        \think\Cookie::set('user_type',$userType);
		$paylist = $createlist = [];
		for ($i = 0; $i < 7; $i++)
		{
			$time = $seventtime + ($i * 86400);
		    $day = date("Y-m-d", $time);
			$start = $time;
			$end = $time + 86400;
			// 订单数
		    $createlist[$day] = model('app\index\model\wanlshop\Order')
				->where('createtime','between', [$start,$end])
				->where(['shop_id' => $shop_id])
				->count();
			// 成交数
		    $paylist[$day] = model('app\index\model\wanlshop\Order')
				->where('createtime','between', [$start,$end])
				->where('createtime','in', '4,6')
				->where(['shop_id' => $shop_id])
				->count();
		}
		
		//七日增长
		$week = model('app\index\model\wanlshop\Order')->whereTime('createtime', 'week')->where('shop_id', $shop_id)->count();
		$lastweek = model('app\index\model\wanlshop\Order')->whereTime('createtime', 'last week')->where('shop_id', $shop_id)->count();
		if($week != 0 && $lastweek != 0 ){
			$sevendnu = bcdiv(bcmul(100, bcsub($week, $lastweek, 2), 2), $lastweek, 2);
		}else{
			$sevendnu = 0;
		}

		//今日订单
		$orderCount = model('app\index\model\wanlshop\Order')->whereIn('state', '2,3,4,6')->whereTime('createtime', 'today')->where('shop_id', $shop_id)->count();
		//今天拼团订单
        $orderGroupsCount = (new \app\index\model\wanlshop\groups\Order)->whereIn('state', '2,3,4,5,6')->whereTime('createtime', 'today')->where('shop_id', $shop_id)->count();
        //今日秒杀订单
        $orderSeckillCount = (new \app\index\model\wanlshop\seckill\Order)->whereIn('state', '2,3,4,5,6')->whereTime('createtime', 'today')->where('shop_id', $shop_id)->count();

        //订单总数
        $totalorder = model('app\index\model\wanlshop\Order')->whereIn('state', '2,3,4,6')->where('shop_id', $shop_id)->count();

        //拼团订单总数
        $totalorderGroups = (new \app\index\model\wanlshop\groups\Order)->whereIn('state', '2,3,4,5,6')->where('shop_id', $shop_id)->count();

        //秒杀订单总数
        $totalorderSeckill = (new \app\index\model\wanlshop\seckill\Order)->whereIn('state', '2,3,4,5,6')->where('shop_id', $shop_id)->count();
        $refund = (new \app\admin\model\wanlshop\Refund)->where('state', '4')->where('shop_id', $shop_id)->sum('price');
        $this->view->assign([
            'totaluser'        => model('app\index\model\wanlshop\Goods')->where('shop_id', $shop_id)->count(), //商品总数
            'totalviews'       => model('app\index\model\wanlshop\Goods')->where('shop_id', $shop_id)->sum('views'), //总访问数
            'totalorder'       => $totalorder + $totalorderGroups +$totalorderSeckill, // 总订单数
            'totalorderamount' => model('app\index\model\wanlshop\Pay')->where('shop_id', $shop_id)->where('pay_state',1)->sum('price') - $refund, //总金额
            'todayuserlogin'   => model('app\api\model\wanlshop\Cart')->where('shop_id', $shop_id)->count(), // 加购物车
			'todayusersignup'  => model('app\api\model\wanlshop\find\Follow')->where('user_no', $this->shop->user_no)->count(), //关注店铺表
            'todayorder'       => $orderCount + $orderGroupsCount + $orderSeckillCount, // 今日订单数量
            'unsettleorder'    => model('app\index\model\wanlshop\Order')->whereIn('state', '2,3,4,6')->whereTime('createtime', 'today')->where(['shop_id' => $shop_id, 'state' => 2])->count(), // 未处理数量
            'sevendnu'         => $sevendnu.'%', 
            'sevendau'         => model('app\index\model\wanlshop\Pay')->where('pay_state', '=',1)->whereTime('createtime', 'today')->where('shop_id', $shop_id)->sum('price') - (new \app\admin\model\wanlshop\Refund)->where('state', '4')->where('shop_id', $shop_id)->whereTime('createtime', 'today')->sum('price'), //今日销售额
            'paylist'          => $paylist,
            'createlist'       => $createlist
        ]);
        return $this->view->fetch();
    }
}


